package com.app.controller;

import com.app.db.mongodb.MongoUserEntity;
import com.app.service.MongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/dev")
public class DevController {

    @Autowired
    MongoService mongoService;

    @RequestMapping("/mongo/save")
    public String save(
            @RequestParam(required = false) Long id,
            HttpServletRequest request){
        mongoService.save(id);
        return "ok";
    }

    @RequestMapping("/mongo/update")
    public String update( HttpServletRequest request){
        Long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        mongoService.update(id,name);
        return "ok";
    }

    @RequestMapping("/mongo/del")
    public String del( HttpServletRequest request){
        Long id = Long.parseLong(request.getParameter("id"));
        mongoService.del(id);
        return "ok";
    }

    @RequestMapping("/mongo/get")
    public List<MongoUserEntity> get(HttpServletRequest request){
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        String name = request.getParameter("name");
        return mongoService.get(pageSize,pageNum,name);
    }
}
